package com.wuming.house.controller;

import com.wuming.house.core.CommonResult;
import com.wuming.house.core.JsonResult;
import com.wuming.house.model.Aboutus;
import com.wuming.house.service.AboutusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/web/aboutus")
public class AboutusController extends BaseController {

    @Autowired
    private AboutusService aboutusService;

    @RequestMapping("/toAboutus")
    public String toAboutus() {
        return "/tools/manageAboutus";
    }

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult list() {
        try {
            List<Aboutus> usLists = new ArrayList<>();
            Aboutus us = aboutusService.getAboutusById(1L);
            usLists.add(us);
            return CommonResult.createSuccessResult(usLists, usLists.size(), "查询关于我们成功");
        } catch (Exception e) {
            logger.error("发生业务异常,异常信息:{}", e.getMessage());
        }
        return CommonResult.createFailureResult("查询关于我们失败");
    }


    @ResponseBody
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public CommonResult get(@PathVariable Long id) {
        try {
            Aboutus us = aboutusService.getAboutusById(1L);
            return CommonResult.createSuccessResult(us, 1, "查询关于我们成功");
        } catch (Exception e) {
            logger.error("发生业务异常,异常信息:{}", e.getMessage());
        }
        return CommonResult.createFailureResult("查询关于我们失败");
    }

    @RequestMapping("/toEditAboutus/{id}")
    public String toEditActivity(@PathVariable Long id, Model model) {
        Aboutus us = aboutusService.getAboutusById(id);
        model.addAttribute("us", us);
        return "/tools/editAboutus";
    }


    @ResponseBody
    @RequestMapping(value = "/save")
    public JsonResult save(Aboutus us) {
        if (null == us.getId()) {
            us.setCreateDatetime(new Date());
            aboutusService.saveAboutus(us);
            return new JsonResult(us, "添加关于我们成功");
        } else {
            aboutusService.modifyAboutus(us);
            return new JsonResult(us, "更新关于我们成功");
        }
    }
}
